<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="./lib/vue.js" type="text/javascript" charset="utf-8"></script>
        <title>Document</title>
    </head>
    <body>
        <div id="app">
            <ul>
                <!-- 
                    因为父元素的方法可以直接修改父元素的数据
                    所以将父元素的方法传递给子元素
                    然而由子元素进行调用,从而修改父元素的数据
                 -->
                <school v-for="(item,index) in schoolList" :action="changEvent" :key="'abc'+index" :index="index" :school-name="item"></school>
            </ul>
            <h2>选择的学校是:{{ chooseSchool }}</h2>
        </div>
    </body>
    <script>
        Vue.component("school",{
            props: ['schoolName','index','action'],
            template:`<li>
                    <h3>{{index}}--学校:{{ schoolName }}</h3>
                    <button @click="clickEvent(schoolName)">选择</button>
                </li>`,
            methods: {
                clickEvent:function(schoolName){
                    console.log(schoolName);
                    // console.log(this.action);
                    console.log(this);
                    this.action(schoolName);// 把父元素的方法传给子组件,子组件通过调用:action="changEvent"方法,来修改父元素的数据
                }
            }
        })
        let app = new Vue({
            el:"#app",
            data:{
                schoolList:[
                    "小学","中学","高中","大学"
                ],
                chooseSchool:''
            },
            methods: {
                changEvent:function(data){// 这里data接收的数据是this.$emit('cschool',schoolName);  schoolNmae的数据
                    console.log("触发学校选择事件");
                    // 拿到数据就可以修改
                    this.chooseSchool = data
                }
            }
        })
    </script>
</html>